﻿
import { Dialog } from 'primeng/components/dialog/dialog';
import { Component, Injector, ViewChild, OnInit } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { RoleListDto, RoleServiceProxy, ContactDto, ContactServiceProxy, TemplateServiceProxy, WorkStationDto, WorkStationUpdateOrOutput, WorkingTeamServiceProxy, WorkingTeamDto, CreateWorkingTeam } from '@shared/service-proxies/service-proxies';
import { Table } from 'primeng/components/table/table';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';


@Component({
    selector : 'app-contact',
    templateUrl: './working-team.component.html',
    styleUrls: ['./working-team.component.less'],
    animations: [appModuleAnimation()]
})
export class WorkingTeamComponent extends AppComponentBase implements OnInit  {

    @ViewChild('dt') dataTable: Table;

    displayDialog: boolean;

    workingTeam: WorkingTeamDto;    
    selectedWorkingTeam: WorkingTeamDto;    
    newWorkingTeam: boolean;    
    workingTeams: WorkingTeamDto[];

    constructor(
        injector: Injector,
        private _workingTeamServiceProxy: WorkingTeamServiceProxy
        ) 
        {
            super(injector);            
        }
    
    
    cols: any[];
    ngOnInit(): void {
            
        this.getAllWorkingTeam();
        this.cols = [
            { field: 'id', header: '编号' },
            { field: 'displayName', header: '小组名称' }
        ];
    }

    getAllWorkingTeam()
    {
        this._workingTeamServiceProxy.getAll('','','',0,1000)
        .subscribe(result => {
            this.workingTeams = result.items;
          });
        
    }

    showDialogToAdd() {
        this.newWorkingTeam = true;
        this.workingTeam = new WorkingTeamDto();
        this.displayDialog = true;
    }


    save() {       

         if(this.newWorkingTeam){            
            let createWorkingTeam = new CreateWorkingTeam();
            createWorkingTeam.displayName = this.workingTeam.displayName;
            createWorkingTeam.memberList = this.workingTeam.memberList;
            this._workingTeamServiceProxy.create(createWorkingTeam)
            .subscribe(result => {
             
                this.notify.info('创建成功！');
                this.getAllWorkingTeam();
            });
        }else
        {
            this._workingTeamServiceProxy.update(this.workingTeam)
            .subscribe(() => {              
                this.notify.info('修改成功');
                this.getAllWorkingTeam();
            });
        }

       
        this.displayDialog = false;   
        
    }

    delete() {
        this._workingTeamServiceProxy.delete(this.selectedWorkingTeam.id)
        .subscribe(result => {
            this.notify.info('删除成功');
            this.getAllWorkingTeam();
          });
        
        this.workingTeam = null;
        this.displayDialog = false;
    }

    onRowSelect(event) {
        this.newWorkingTeam = false;
        this.workingTeam = this.cloneStation(event.data);
        this.displayDialog = true;
    }

    cloneStation(c: WorkingTeamDto): WorkingTeamDto {
        let station = new WorkingTeamDto();
        for (let prop in c) {
            station[prop] = c[prop];
        }
        return station;
    }

      

}
